package py.com.fms.com.persistence.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

import py.com.fms.com.bean.OrderHeadBean;
import py.com.fms.com.bean.OrderDetBean;
import py.com.fms.exception.CommonException;
import py.com.fms.fms.bean.PersonBean;
import py.com.fms.persistence.services.FmsServices;

@SuppressWarnings("unused")
public class OrderServices extends FmsServices {

	public OrderServices(SqlMapClient client) {
		super(client);
		// TODO Auto-generated constructor stub
	}
	
	/**
	 [[[
	 [[ The following method retrieve a list of all records
	 [[ Normally used to load a grid.
	 */
	
	/**
	 * Returns an List object with all data of the Entity Header. 
	 *
	 * @return      a list with all data
	 */
	@SuppressWarnings("unchecked")
	public List<OrderHeadBean> getAllOrderHead() throws CommonException{
		List<OrderHeadBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<OrderHeadBean>)this.getConfig().queryForList("order.getAllOrderHead");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns an List of Orders for a specify supplier. 
	 * The argument must be a Long Object with the value of the Supplier Id
	 *
	 * @param  		the supplier id
	 * @return      a list with the orders for the passed argument (Supplier Id)
	 */
	@SuppressWarnings("unchecked")
	public List<OrderHeadBean> getOrderBySupplierId(Long supplierId) throws CommonException{
		List<OrderHeadBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<OrderHeadBean>)this.getConfig().queryForList("order.getOrderBySupplierId", supplierId);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns Pending orders. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the supplier id
	 * - A String object containing the order status
	 * - A Date Object containing the order date
	 * 
	 * @param  		the supplier id
	 * @param  		the order status
	 * @param  		the order Date
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<OrderHeadBean> getFilteredOrders(Long idSupplier, String status, Date orderDate) throws CommonException, SQLException{
		List<OrderHeadBean> queryForList;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idSupplier", idSupplier);
			param.put("status", status);
			param.put("orderDate", orderDate);
			queryForList = (List<OrderHeadBean>)this.getConfig().queryForList("order.getFilteredOrders", param);
			return queryForList;
		} catch (SQLException e) {
			this.getConfig().endTransaction();
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns Pending orders. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the supplier id
	 * - A Date Object containing the order date
	 * 
	 * @param  		the supplier id
	 * @param  		the order Date
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<OrderHeadBean> getPendingOrders(Long idSupplier, Date orderDate, String status) throws CommonException, SQLException{
		List<OrderHeadBean> queryForList;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idSupplier", idSupplier);
			param.put("orderDate", orderDate);
			param.put("status", status);
			queryForList = (List<OrderHeadBean>)this.getConfig().queryForList("order.getPendingOrders", param);
			return queryForList;
		} catch (SQLException e) {
			this.getConfig().endTransaction();
			throw new CommonException(e);
		}
	}
	
	
	/**
	 * Returns an List object with all data of the Entity Detail. 
	 *
	 * @return      a list with all data
	 */
	@SuppressWarnings("unchecked")
	public List<OrderDetBean> getAllOrderDet() throws CommonException{
		List<OrderDetBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<OrderDetBean>)this.getConfig().queryForList("order.getAllOrderDet");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns an List of Details for a Header Id. 
	 * The argument must be a Long Object with the value of the header Id
	 *
	 * @param  		value of the header id
	 * @return      a list with the details for the passed argument (header Id)
	 */
	@SuppressWarnings("unchecked")
	public List<OrderDetBean> getOrderDetByHeadId(Long headId) throws CommonException{
		List<OrderDetBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis]] 
			
			queryForList = (List<OrderDetBean>)this.getConfig().queryForList("order.getOrderDetByHeadId", headId);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	/**
	 * Returns an specific header object. 
	 * The argument must be a Long Object with the value of the header Id
	 *
	 * @param  		value of the header id
	 * @return      the header object for the passed argument (id)
	 */
	public OrderHeadBean getOrderHeadById(Long id) throws CommonException{
		OrderHeadBean queryForObject;
		try {
			
			//[[ queryForObject is an implemented interface of iBatis ]] 
			
			queryForObject = (OrderHeadBean)this.getConfig().queryForObject("order.getOrderHeadById", id);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 * Returns an specific header object. 
	 * The passed arguments are:
	 * <p>
	 * - A Long object containing the value of the enterprise id
	 * - A long Object containing the order number
	 * 
	 * @param  		the enterprise id
	 * @param  		the order number
	 * @return      the header object for the passed arguments
	 */
	public Long getOrderHeadIdByOrderNumber(Long idEnt, Long orderNumber) throws CommonException, SQLException{
		Long queryForObjectLong;
		try {
			HashMap< String, Object> param = new HashMap<String, Object>();
			param.put("idEnt", idEnt);
			param.put("orderNumber", orderNumber);
			queryForObjectLong = (Long)this.getConfig().queryForObject("order.getOrderHeadIdByEntOrderNum", param);
			return queryForObjectLong;
		} catch (SQLException e) {
			this.getConfig().endTransaction();
			throw new CommonException(e);
		}
	}
	
	/**
	 * Insert a Header Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the header Fields
	 * 
	 * @param  		the header entity bean(OrderHeadBean)
	 */
	public void saveNewOrderHead(OrderHeadBean orderHead) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("order.insOrderHead", orderHead);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 * Insert Detail Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the detail fields including the header id
	 * 
	 * @param  		the detail entity bean(OrderDetBean)
	 */
	public void saveNewOrderDet(OrderDetBean orderDet) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("order.insOrderDet", orderDet);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
	/**
	 * Update a Header Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the header Fields
	 * 
	 * @param  		the header entity bean(OrderHeadBean)
	 */
	public void updateOrderHead(OrderHeadBean orderHead) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("order.udpOrderHeadById", orderHead);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
	
	
	/**
	 * update a Detail Record. 
	 * The passed argument is:
	 * <p>
	 * - A entity Object containing the detail fields including the header id
	 * 
	 * @param  		the detail entity bean(OrderDetBean)
	 */
	public void updateOrderDet(OrderDetBean orderDet) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("order.udpOrderDetById", orderDet);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	

	/**
	 * update a Detail Invoice Quantities. 
	 * The passed argument is:
	 * <p>
	 * - The Header Id
	 * - The Line Id
	 * 
	 * @param  		Long headerId
	 * @param  		Long item
	 * @param  		Double quantity
	 */
	public void updateInvoiceQuantities(Long headerId, Long item, Double quantity, String status) throws CommonException{
		try {
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("idHead", headerId);
			param.put("item", item);
			param.put("quantity", quantity);
			param.put("status", status);
			this.getConfig().startTransaction();
			this.getConfig().update("order.udpInvoiceQuantities", param);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
	/**
	 * Confirm a specific order. 
	 * The passed argument is:
	 * <p>
	 * - A Long object with the order id
	 * 
	 * @param  		the order id
	 */
	public void confirmOrder(Long idOrder) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("order.confirmOrderById", idOrder);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 * Cancel a specific order. 
	 * The passed argument is:
	 * <p>
	 * - A Long object with the order id
	 * 
	 * @param  		the order id
	 */
	public void cancelOrder(Long idOrder) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("order.cancelOrderById", idOrder);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	
	

}
